package com.aaa.controller;

import com.aaa.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

// @Controller+@ResponseBody
// 所有方法默认添加了@ResponseBody，返回json
@RestController
@RequestMapping("auth")
public class AuthUserController {

    @RequestMapping("m4")
    public Object m4(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }

    @RequestMapping("m1")
    public Object m1(Authentication authentication){
        System.out.println(authentication);
        Object principal = authentication.getPrincipal();// 主体对象：认证时返回的UserDetails
        System.out.println(principal);
        System.out.println(principal.getClass());
        User tuserDetails = (User) principal;
        return authentication;
    }

    @RequestMapping("m2")
    public Object m2(Principal principal){
        // UsernamePasswordAuthenticationToken
        System.out.println(principal.getClass());
        System.out.println(principal);
        return principal;
    }

    @RequestMapping("m3")
    public Object m3(HttpServletRequest request){
        Principal userPrincipal = request.getUserPrincipal();
        System.out.println(userPrincipal);
        System.out.println(userPrincipal.getClass());
        return userPrincipal;
    }

}
